package com.ruoyi.framework.interceptor;

import cn.dev33.satoken.stp.StpInterface;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.framework.web.service.SysPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 自定义权限加载接口实现类
 */
@Component
public class StpInterfaceImpl implements StpInterface {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private SysPermissionService permissionService;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        SysUser user = userService.selectUserById(Long.valueOf(loginId.toString()));
        Set<String> menuPermission = permissionService.getMenuPermission(user);
        return new ArrayList<>(menuPermission);
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        SysUser user = userService.selectUserById(Long.valueOf(loginId.toString()));
        Set<String> rolePermission = permissionService.getRolePermission(user);
        return new ArrayList<>(rolePermission);
    }
}
